/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-07-20 17:46:20
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-27 09:53:36
 * @FilePath: \ems_neutral_app\lib\pages\Login\ForgetPwdGuidPage.dart
 * @Description: 忘记密码+引导页
 */
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../bloc/userbloc/user_bloc.dart';
import '../../common/Method/CommonMethod.dart';
import '../../common/widgets/cubitWidget/BottomSheetView.dart';
import '../../common/widgets/cubitWidget/InputView.dart';
import '../../common/widgets/cubitWidget/SubmitBtn.dart';
import '../../config/AppInfo.dart';
import '../../utils/color/ColorUtils.dart';
import '../../utils/router/ARouter.dart';
import '../Mine/Language/LanguageController.dart';
import '../Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'Controller/LoginController.dart';
import 'ForgetPwdSubmitPage.dart';
import 'LoginGuidPage.dart';
import 'View/LoginRegisterItem.dart';
import 'View/LoginRichTextView.dart';

class ForgetPwdGuidPage extends StatefulWidget {
  const ForgetPwdGuidPage({super.key});

  @override
  State<ForgetPwdGuidPage> createState() => _ForgetPwdGuidPageState();
}

class _ForgetPwdGuidPageState extends State<ForgetPwdGuidPage> {
  String? _email;
  String? _phone;
  String? _code;

  /// 区号
  late String _areaCode;
  late List<String> _telCodeList;
  bool _choosePhoneBack = false; // 默认选择邮箱找回
  LoginController _loginController = Get.put(LoginController());
  LanguageController _languageController =
      Get.put<LanguageController>(LanguageController());
  bool _isPromptEnglish = true;

  @override
  void initState() {
    super.initState();

    String? temp = UserBlocTool.userEntity?.areaCode;
    _areaCode = temp ?? '+86';
    _telCodeList = kTelCodeList;
    _isPromptEnglish = _languageController.isPromptEnglish();
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      sideMargin: 40.w,
      title: LangOf(context)?.login?.forgetpwd,
      body: mainWidget(),
    );
  }

  mainWidget() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
            child: ListView(
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          padding: EdgeInsets.symmetric(vertical: 40.w),
          children: [
            /// 选择注册方式
            if (!_isPromptEnglish) chooseBackModeView(),

            /// 邮箱
            if (_choosePhoneBack == false)
              LoginRegisterItem(
                imgPath: 'imgs/login/login_zh.png',
                hintText: LangOf(context)?.register?.emailPlaceholder,
                inputViewType: InputViewType.InputViewTypeEmail,
                onValid: (p0, p1) {
                  _email = p1;
                  setState(() {});
                },
              ),

            /// 手机
            if (_choosePhoneBack == true) phoneInputView(),

            /// 验证码
            LoginRegisterItem(
              imgPath: 'imgs/login/login_yzm.png',
              hintText: LangOf(context)?.register?.codePlaceholder,
              inputViewType: InputViewType.InputViewTypeCode,
              account: _choosePhoneBack ? _phone : _email,
              showSendCode: true,
              codeType: _choosePhoneBack
                  ? CodeType.CodeTypeByPhone
                  : CodeType.CodeTypeByEmail,
              onValid: (p0, p1) {
                _code = p1;
                setState(() {});
              },
            ),

            /// 提交
            submitBtnWidget(),
          ],
        )),
        LoginRichTextView(
          margin: EdgeInsets.only(bottom: 60.w),
          leftTitle: LangOf(context)?.bindingZH?.remember_pwd,
          rightTitle: LangOf(context)?.bindingZH?.goLogin,
          onTap: () => ARoute.gotoPage(context, LoginGuidPage()),
        )
      ],
    );
  }

  phoneInputView() {
    Widget letfWidget = Row(
      children: [
        SilImage(
          'imgs/login/login_sj.png',
          height: 28.w,
          width: 28.w,
          margin: EdgeInsets.only(right: 8.w),
        ),
        SilButton(
          padding: EdgeInsets.only(right: 5.w),
          imgPath: 'imgs/common/common_sanjx.png',
          imgHeight: 15.w,
          imgWidth: 15.w,
          title: _areaCode,
          titleColor: ColorUtils.color_font_main,
          fontSize: 14.sp,
          margin: EdgeInsets.only(right: 10.w),
          onTapFunc: showTelCodeListPop,
        ),
        Container(
          height: 28.w,
          width: 0.5.w,
          margin: EdgeInsets.only(top: 3.w, bottom: 3.w, right: 18.w),
          color: ColorUtils.hexColor('#333333'),
        ),
      ],
    );

    return InputView(
        margin: EdgeInsets.only(top: 30.w),
        inputViewMargin: EdgeInsets.symmetric(horizontal: 8.w),
        title: '',
        hintText: LangOf(context)?.register?.phonePlaceholder,
        inputViewType: InputViewType.InputViewTypePhone,
        leftView: letfWidget,
        hideTitle: true,
        onValid: (valid, value) {
          _phone = value;
          setState(() {});
        });
  }

  showTelCodeListPop() {
    SilBottomSheet.show(
        context: context,
        child: BottomSheetView(
          titles: _telCodeList,
          initialSelectedItem: _telCodeList.indexOf(_areaCode),
          tapSureSeletedFunc: (selected) {
            _areaCode = _telCodeList[selected];
            setState(() {});
          },
        ));
  }

  submitBtnWidget() {
    return Container(
      alignment: Alignment.center,
      child: SubmitBtn(
        margin: EdgeInsets.only(top: 30.w),
        title: LangOf(context)?.bindingZH?.next_step,
        onTapFunc: () {
          // 隐藏键盘
          globalTextFieldDismiss(context);
          _loginController.verifyForgetPwd(
              context: context,
              phone: _phone,
              email: _email,
              code: _code,
              choosePhoneBack: _choosePhoneBack,
              successHandler: () => ARoute.gotoPage(
                  context,
                  ForgetPwdSubmitPage(
                      phone: _phone, email: _email, code: _code)));
        },
      ),
    );
  }

  /// 选择找回方式
  chooseBackModeView() {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          SilButton(
            title: LangOf(context)?.bindingZH?.find_email,
            titleColor: _choosePhoneBack
                ? ColorUtils.color_subtitle
                : ColorUtils.color_font_main,
            onTapFunc: () {
              if (_choosePhoneBack != false) {
                _choosePhoneBack = false;
                setState(() {});
              }
            },
          ),
          SilButton(
            title: LangOf(context)?.bindingZH?.find_phone,
            titleColor: _choosePhoneBack
                ? ColorUtils.color_font_main
                : ColorUtils.color_subtitle,
            onTapFunc: () {
              if (_choosePhoneBack != true) {
                _choosePhoneBack = true;
                setState(() {});
              }
            },
          ),
        ],
      ),
    );
  }
}
